define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'aprcard/index',
                    add_url: 'aprcard/add',
                    edit_url: 'aprcard/edit',
                    del_url: 'aprcard/del',
                    multi_url: 'aprcard/multi',
                    table: 'cards',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'cardId',
                sortName: 'cardId',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'cardId', title: __('CardId')},
                        {field: 'user.phone', title: __('UserId')},
                        //{field: 'type', title: __('Type')},
                        {field: 'apr', title: __('Apr'),formatter:Controller.api.formatter.apr},
                        //{field: 'apr', title: __('Apr')},
                        {field: 'isUse', title: __('IsUse'),formatter:Controller.api.formatter.isUse},
                        {field: 'createTime', title: __('CreateTime'), formatter: Table.api.formatter.datetime},
                        {field: 'endTime', title: __('EndTime'), formatter: Table.api.formatter.datetime},
                        {field: 'limitMoney', title: __('LimitMoney')},
                        {field: 'limitDay', title: __('LimitDay')},
                        {field: 'actType', title: __('ActType'),formatter:Controller.api.formatter.actType},
                        {field: 'stuff', title: __('Stuff')},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                isUse : function(value, row, index){
                    if(value==0){
                        return "未使用";
                    }else{
                        return "已使用";
                    }
                },
                apr : function(value, row, index){
                    return value+'%';
                },
                actType : function(value, row, index){
                    if(value==1){
                        return "注册红包";
                    }
                    if(value==2){
                        return "后台添加";
                    }
                },
            }
        }
    };
    return Controller;
});